←Select platform

GetYUVData(RasterYUVFormat,byte[],int,int) Method

Summary
Converts the RasterImage grayscale or BGR data to YUV and stores it in an output buffer.
Syntax
C#
C++/CLI
Python
public int GetYUVData( 
   RasterYUVFormat yuvFormat, 
   byte[] buffer, 
   int bufferIndex, 
   int bufferCount 
) 
public: 
int GetYUVData(  
   RasterYUVFormat yuvFormat, 
   array<byte>^ buffer, 
   int bufferIndex, 
   int bufferCount 
)  
def GetYUVData(self,yuvFormat,buffer,bufferIndex,bufferCount): 

Parameters

yuvFormat
Specifies the YUV data format.

buffer
Buffer to receive the YUV data. If NULL, this method will return the expected size of the YUV data buffer. If not NULL, the YUV data will be extracted and stored in buffer if the buffer is large enough to contain the YUV data.

bufferIndex
The zero-based index into the  buffer where the set operation should start.

bufferCount
Size of the data buffer pointed to by buffer. If this value is incorrect, this method will throw an exception.

Return Value

The expected size of the YUV data buffer.

Remarks

The user is responsible for allocating and freeing the YUV buffer when it is no longer needed.

The buffer should be large enough to store the data for the whole image. If the buffer is not large enough to store all the requested data, the method will throw and exception.

Note that in each case, the function ignores the ViewPerspective. So the YUV data will match the view perspective in the image. In other words, if the image's view perspective is BottomLeft, the YUV data you retrieve in the YUV buffer will be flipped. In most cases, you will want the YUV buffer to be correct side up, so you will want ViewPerspective to be TopLeft. You can use ChangeViewPerspective to make the bitmap be the correct ViewPerspective.

The YUV data will be in studio YUV format, which means the Y values will range from 16 to 235 and U, V values will range from 16 to 240. The Y values are unsigned, while U, V values are offset by 0x80. In other words, a value of 0x80 for U corresponds to a U value of 0 and a value of 0x7F corresponds to a U value of -1.

If you do not know the expected size of the YUV buffer, you can call the function twice, as follows:

long dataSize = image.GetYUVData(RasterYUVFormat.NV12, IntPtr.Zero, 0L);  
Byte[] dataBuffer = new byte[size];  
image.GetYUVData(RasterYUVFormat.YV12, yuvBuffer2, 0, yuvBuffer2.Length); 
Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Dicom; 
using Leadtools.Drawing; 
using Leadtools.Controls; 
using Leadtools.Svg; 
 
 
public void SetYUVDataExample() 
{ 
   string fileName = Path.Combine(LEAD_VARS.ImagesDir, "nv21.bin"); 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      using (RasterImage image = new RasterImage(RasterMemoryFlags.Conventional, 1920, 1080, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0)) 
      { 
         using (FileStream yuvFile = File.OpenRead(fileName)) 
         { 
            int size = 1920 * 1080 * 3 / 2; 
 
            Byte[] yuvBuffer = new Byte[size]; 
            yuvFile.Read(yuvBuffer, 0, size); 
 
            image.SetYUVData(yuvBuffer, 0, size, RasterYUVFormat.NV21); 
 
            codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "nv21_net.jpg"), RasterImageFormat.Jpeg, 0); 
 
            Byte[] yuvBuffer2 = new byte[size]; 
            image.GetYUVData(RasterYUVFormat.YV12, yuvBuffer2, 0, yuvBuffer2.Length); 
 
            image.SetYUVData(yuvBuffer2, 0, yuvBuffer2.Length, RasterYUVFormat.YV12); 
 
            codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "nv21_net_2.jpg"), RasterImageFormat.Jpeg, 0); 
 
            System.Diagnostics.Debug.WriteLine("Save succeeded"); 
         } 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.